﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Entity;
using BO;

namespace Final_Prototype_UsingDev_QuanLy_071012
{
    public partial class Main : Form
    {
        CustomerBO csBO = new CustomerBO();
        PermissionBO pBO = new PermissionBO();
        StaffBO sBO = new StaffBO();
        ManagedByBO mBO = new ManagedByBO();

        public PermissionEntity _permission = new PermissionEntity();
        public UserEntity _user = new UserEntity();
        public StaffEntity _staff = new StaffEntity();

        public delegate void delPassData(string sData);
        public delegate void delUserInfo(UserEntity user);

        public Main()
        {
            InitializeComponent();
        }


        /// <summary>
        /// load danh sach khach hang
        /// </summary>
        public void ReceiveUserInfo(UserEntity user)
        {
            _user = user;
        }

        /// <summary>
        /// load danh sach khach hang
        /// </summary>
        public void LoadCustomer()
        {
            grdCustomerInfo.DataSource = csBO.LoadCustomer();
            for (int i = 11; i < grdCustomerInfo.Columns.Count; i++)
                grdCustomerInfo.Columns[i].Visible = false;
        }

        /// <summary>
        /// load danh sach khach hang theo sale
        /// </summary>
        public void LoadCustomerBy()
        {
            grdCustomerInfo.DataSource = csBO.LoadCustomerBySaleName(_user.StaffName);
            for (int i = 11; i < grdCustomerInfo.Columns.Count; i++)
                grdCustomerInfo.Columns[i].Visible = false;
        }

        /// <summary>
        /// Load quyen cua tai khoan dang nhap
        /// </summary>
        private void LoadPermission()
        {
            _permission = pBO.LoadPermission(_user.GroupName);
        }

        /// <summary>
        /// Load quyen cua tai khoan dang nhap
        /// </summary>
        private void LoadStaffInfo(string staffname)
        {
            _staff = sBO.LoadStaffInfo(staffname);
        }

        /// <summary>
        /// Loc danh sach khach hang theo sale name
        /// </summary>
        public void LoadCustomerFromSaleName()
        {
            string FromSaleName = cboChooseSaleMan.Text;
            if (FromSaleName != "")
                grdCustomerFrom.DataSource = csBO.LoadCustomerBySaleName(FromSaleName);
            else
            {
                grdCustomerFrom.DataSource = csBO.LoadCustomer();
                grpFromSale.Text = "All customer";
            }
            for (int i = 1; i < grdCustomerFrom.Columns.Count; i++)
                grdCustomerFrom.Columns[i].Visible = false;
            grdCustomerFrom.Columns[1].Visible = true;
            grdCustomerFrom.Columns[grdCustomerFrom.Columns.Count - 1].Visible = true;
            grdCustomerFrom.Columns[grdCustomerFrom.Columns.Count - 2].Visible = true;


        }

        /// <summary>
        /// Loc danh sach khach hang theo sale name
        /// </summary>
        public void LoadCustomerToSaleName()
        {

            string ToSaleName = cboToSaleMan.Text;
            grdCustomerTo.DataSource = csBO.LoadCustomerBySaleName(ToSaleName);

            for (int i = 0; i < grdCustomerTo.Columns.Count; i++)
                grdCustomerTo.Columns[i].Visible = false;
            grdCustomerTo.Columns[0].Visible = true;
            grdCustomerTo.Columns[grdCustomerTo.Columns.Count - 1].Visible = true;
            grdCustomerTo.Columns[grdCustomerTo.Columns.Count - 2].Visible = true;
        }

        /// <summary>
        /// load danh sach sale name duoc chon
        /// </summary>
        private void LoadFromSaleName()
        {
            cboChooseSaleMan.DataSource = sBO.LoadStaff();
            cboChooseSaleMan.DisplayMember = "StaffName";
            cboChooseSaleMan.Text = _user.StaffName;
            if (!_permission.AllPermission)
            {
                cboChooseSaleMan.Enabled = false;
            }

        }

        /// <summary>
        /// load danh sach sale name duoc uy quyen
        /// </summary>
        public void LoadToSaleName()
        {
            string SaleName = cboChooseSaleMan.Text.ToString();
            cboToSaleMan.DataSource = sBO.LoadStaff(SaleName);
            cboToSaleMan.DisplayMember = "StaffName";
            cboToSaleMan.SelectedIndex = -1;

        }

        /// <summary>
        /// Save thong tin khach hang
        /// </summary>
        private void EditAuthorize()
        {
            string ToSale = cboToSaleMan.Text;
            string FromSale, CustomerCode;
            DateTime FromDate = dpFromdate.Value;
            DateTime ToDate = dpTodate.Value;
            foreach (DataGridViewRow row in grdCustomerFrom.Rows)
            {
                if (row.Cells[0].Value != null)
                {
                    int index = row.Index;
                    FromSale = grdCustomerFrom.Rows[index].Cells["StaffName"].Value.ToString();
                    CustomerCode = grdCustomerFrom.Rows[index].Cells["CustomerCode"].Value.ToString();
                    csBO.EditAuthorize(CustomerCode, FromSale, ToSale, FromDate, ToDate);
                }
            }

            MessageBox.Show("Authorized successful!");
        }

        /// <summary>
        /// Xoa khach hang
        /// </summary>
        private void DeleteCustomer()
        {
            DialogResult result = MessageBox.Show("Are you want to delete this customer?", "Warning", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                foreach (DataGridViewRow row in grdCustomerInfo.Rows)
                {
                    if (row.Selected == true)
                    {
                        int index = row.Index;
                        csBO.DeleteCustomer(grdCustomerInfo.Rows[index].Cells["CustomerCode"].Value.ToString().Trim());
                    }
                }
                LoadCustomer();
            }
        }

        /// <summary>
        /// Them sale vao danh sach history sale
        /// </summary>
        public void AddHistorySale()
        {
            ManagedByBO mBO = new ManagedByBO();
            ManagedByEntity info = new ManagedByEntity();
            string ToSale = cboChooseSaleMan.Text;
            string FromSale, CompanyName;
            DateTime FromDate = dpFromdate.Value;
            DateTime ToDate = dpTodate.Value;
            foreach (DataGridViewRow row in grdCustomerFrom.Rows)
            {
                if (row.Cells[0].Value != null)
                {
                    int index = row.Index;
                    FromSale = grdCustomerFrom.Rows[index].Cells["StaffName"].Value.ToString();
                    CompanyName = grdCustomerFrom.Rows[index].Cells["CompanyName"].Value.ToString();
                    LoadStaffInfo(cboChooseSaleMan.Text);

                    info.StaffName = cboChooseSaleMan.Text;
                    //info.CompanyName = CompanyName;
                    //info.Position = _staff.Position;
                    info.Tel = _staff.Tel;
                    info.Email = _staff.Mail;
                    info.ManagedFrom = FromDate;
                    info.ManagedTo = ToDate;

                    mBO.AddHistorySale(info);
                }
            }

            LoadStaffInfo(_user.StaffName);
        }

        /// <summary>
        /// Kiem tra de cap nhat lai danh sach khach hang
        /// </summary>
        public void VerifyAuthorized()
        {

            string FromSale, CustomerCode, CustomerName;
            DateTime ToDate;
            foreach (DataGridViewRow row in grdCustomerInfo.Rows)
            {
                if (row.Cells[0].Value != null)
                {
                    int index = row.Index;
                    ToDate = (DateTime)grdCustomerInfo.Rows[index].Cells["ManagedTo"].Value;
                    if (ToDate < DateTime.Today)
                    {
                        DateTime FromDate = DateTime.Today;

                        FromSale = grdCustomerInfo.Rows[index].Cells["StaffName"].Value.ToString();
                        CustomerCode = grdCustomerInfo.Rows[index].Cells["CustomerCode"].Value.ToString();
                        CustomerName = grdCustomerInfo.Rows[index].Cells["CompanyName"].Value.ToString(); string ToSale = mBO.GetPreviousSale(CustomerName);
                        if (FromSale != ToSale)
                        {
                            csBO.EditAuthorize(CustomerCode, FromSale, ToSale, FromDate, ToDate.AddYears(50));                   
                            MessageBox.Show("Customer " + CustomerName + " is expired of your authorize!");
                        }
                    }
                }
            }
            LoadCustomerBy();
        }


        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }


        private void btnQuotationBox_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

            palQuotationBox.Visible = true;

          

            


        }

        private void btn1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void barEditItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void btnNewQuotation_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            palNewQuotation.Visible = true;
        }

        private void Main_Load(object sender, EventArgs e)
        {

            palBooking.Visible = true;
            palBookingBox.Visible = false;
            palNewBooking.Visible = false;


            palAuthorized.Visible = false;
            grdCustomerInfo.Visible = true;


            grdQuotation.Visible = true;
            palNewQuotation.Visible = false;

            grdBooking.Visible = true;
            palQuotationBox.Visible = false;



            LoadPermission();
            LoadCustomerBy();
            LoadStaffInfo(_user.StaffName);
            VerifyAuthorized();


            if (_permission.View == false)
            {
                btnShowAllCustomer.Enabled = false;
                btnShowSaleCustomer.Enabled = false;
                txtFindCustomer.Enabled = false;
                cboByCustomer.Enabled = false;
            }
            if (_permission.Insert == false)
            {
                btnCreate.Enabled = false;
            }
            if (_permission.Delete == false)
            {
                btnDelete.Enabled = false;
            }
            if (_permission.Update == false)
            {
                btnAuthorized.Enabled = false;
            }



        }

        private void grdCustomerInfo_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void btnCreat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

            Form_Custom_Management_Add fr = new Form_Custom_Management_Add(this);
            fr.Show();

        }

        private void btnSave_Click(object sender, EventArgs e)
        {

        }

        private void btnAuthorized_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            palAuthorized.Visible = true;
            grdCustomerInfo.Visible = false;
            LoadFromSaleName();
            LoadToSaleName();
            grpFromSale.Text = _user.StaffName;
        }

        private void btnCan_Click(object sender, EventArgs e)
        {
            palAuthorized.Visible = false;
            grdCustomerInfo.Visible = true;
        }

        private void tabCategories_Paint(object sender, PaintEventArgs e)
        {

        }

        private void btnShowAllCustomer_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            palAuthorized.Visible = false;
            grdCustomerInfo.Visible = true;
            LoadCustomer();
        }


        private void simpleButton6_Click(object sender, EventArgs e)
        {
            palNewQuotation.Visible = false;
        }

        private void cboTransitType_SelectedIndexChanged(object sender, EventArgs e)
        {
            string check1 = cboTransitType.SelectedItem.ToString();
            if (check1 == "")
            {

                lblCheckTransitType.Visible = true;

            }
            else
            {

                lblCheckTransitType.Visible = false;

            };
            if (checkRequire())
                btnAddTransitType.Enabled = true;
            else
                btnAddTransitType.Enabled = false;
        }

        private void cboFreight_SelectedIndexChanged(object sender, EventArgs e)
        {
            string check1 = cboFreight.SelectedItem.ToString();
            if (check1 == "")
            {

                lblCheckFreight.Visible = true;

            }
            else
            {

                lblCheckFreight.Visible = false;

            };
            if (checkRequire())
                btnAddTransitType.Enabled = true;
            else
                btnAddTransitType.Enabled = false;
        }

        private void txtTransitTime_TextChanged(object sender, EventArgs e)
        {
            if (txtTransitTime.Text == "")
            {
                lblCheckTransitTime.Visible = true;
            }
            else
            {
                lblCheckTransitTime.Visible = false;
                if (checkRequire())
                    btnAddTransitType.Enabled = true;

                else
                    btnAddTransitType.Enabled = false;
            }
        }

        private void txtFrequency_TextChanged(object sender, EventArgs e)
        {
            if (txtFrequency.Text == "")
            {
                lblCheckFrequency.Visible = true;
            }
            else
            {
                lblCheckFrequency.Visible = false;
                if (checkRequire())
                    btnAddTransitType.Enabled = true;

                else
                    btnAddTransitType.Enabled = false;
            }
        }

        private void cboItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            string check1 = cboItems.SelectedItem.ToString();
            if (check1 == "")
            {

                lblCheckItems.Visible = true;

            }
            else
            {

                lblCheckItems.Visible = false;

            };
            if (checkRequire())
                btnAddTransitType.Enabled = true;
            else
                btnAddTransitType.Enabled = false;
        }

        private void cboCurrency_SelectedIndexChanged(object sender, EventArgs e)
        {
            string check1 = cboCurrency.SelectedItem.ToString();
            if (check1 == "")
            {

                lblCheckCurrency.Visible = true;

            }
            else
            {

                lblCheckCurrency.Visible = false;

            };
            if (checkRequire())
                btnAddTransitType.Enabled = true;
            else
                btnAddTransitType.Enabled = false;
        }

        private void cboUnit1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string check1 = cboUnit1.SelectedItem.ToString();
            if (check1 == "")
            {

                lblCheckUnit1.Visible = true;

            }
            else
            {

                lblCheckUnit1.Visible = false;

            };
            if (checkRequire())
                btnAddTransitType.Enabled = true;
            else
                btnAddTransitType.Enabled = false;
        }

        //CHECK REQUIRE FUCTION
        private bool checkRequire()
        {
            int temp = txtTransitTime.Text.Length * txtFrequency.Text.Length * cboTransitType.Text.Length * cboFreight.Text.Length * cboItems.Text.Length * cboCurrency.Text.Length * cboUnit1.Text.Length;
            if (temp != 0)
                return true;

            return false;
        }

        private void btnCreateNewUser_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Form fr1 = new Form16();
            fr1.Show();
        }

        private void btnSetPermission_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Form fr1 = new Form17();
            fr1.Show();
        }

        private void grdQuotation_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void grpCus_Paint(object sender, PaintEventArgs e)
        {

        }

        private void btnShowSaleCustomer_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            palAuthorized.Visible = false;
            grdCustomerInfo.Visible = true;
            LoadCustomerBy();
        }

        private void grdCustomerInfo_CellClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void grdCustomerInfo_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Form_Custom_Management_Edit fr2 = new Form_Custom_Management_Edit(this);
            string sFormName, sStaff, sCompany;
            foreach (DataGridViewRow row in grdCustomerInfo.Rows)
            {
                if (row.Selected == true)
                {
                    int index = row.Index;
                    sFormName = grdCustomerInfo.Rows[index].Cells["CustomerCode"].Value.ToString();
                    sStaff = grdCustomerInfo.Rows[index].Cells["StaffName"].Value.ToString();
                    sCompany = grdCustomerInfo.Rows[index].Cells["CompanyName"].Value.ToString();

                    // Check permission
                    if (_permission.AllPermission == true || (_permission.View != false & sStaff == _user.StaffName))
                    {
                        fr2.Text = sCompany;
                        delPassData del = new delPassData(fr2.FillInfo);
                        del(sFormName);
                        fr2.Show();
                    }
                    else
                    {
                        MessageBox.Show("You are not permission to view " + sCompany + "!");
                    }
                }
            }

        }

        private void txtFindCustomer_TextChanged(object sender, EventArgs e)
        {
            string sBy = cboByCustomer.Text;
            string sFind = txtFindCustomer.Text;
            if (sBy == "" || sBy == "All field")
            {
                grdCustomerInfo.DataSource = csBO.FindCustomer(sFind);
            }
            else
            {
                grdCustomerInfo.DataSource = csBO.FindCustomerBy(sFind, sBy);
            }
        }

        private void cboByCustomer_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sBy = cboByCustomer.Text;
            string sFind = txtFindCustomer.Text;
            if (sBy == "" || sBy == "All field")
            {
                grdCustomerInfo.DataSource = csBO.FindCustomer(sFind);
            }
            else
            {
                grdCustomerInfo.DataSource = csBO.FindCustomerBy(sFind, sBy);
            }
        }

        private void cboChooseSaleMan_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadCustomerFromSaleName();
            LoadToSaleName();
            grpFromSale.Text = cboChooseSaleMan.Text;
        }

        private void cboToSaleMan_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadCustomerToSaleName();
            grpToSale.Text = cboToSaleMan.Text;
            //LoadFromSaleName();
        }

        private void btnSetAuthorized_Click(object sender, EventArgs e)
        {
            EditAuthorize();
            AddHistorySale();
            LoadCustomerFromSaleName();
            LoadCustomerToSaleName();

        }

        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DeleteCustomer();
        }

        private void btnAddNewQuotation_Click(object sender, EventArgs e)
        {
            palNewQuotation.Visible = false;
                
        }

        private void rbc_Click(object sender, EventArgs e)
        {

        }

        private void palQuotationBox_Paint(object sender, PaintEventArgs e)
        {

        }

        private void tabBookingManagement_Paint(object sender, PaintEventArgs e)
        {

        }

        private void btnNewBooking_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            palNewBooking.Visible = true;

        }

        private void btnCan_Click_1(object sender, EventArgs e)
        {
            palBooking.Visible = true;
            palNewBooking.Visible = false;
            palQuotationBox.Visible = false;
        }

        private void btnCancelQuotationBox_Click(object sender, EventArgs e)
        {
            palBooking.Visible = true;
            palNewBooking.Visible = false;
            palQuotationBox.Visible = false;
          
        }


    }
}
